/*
* Copyright 2010 Pavel Kozhin 
*
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed  under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License.
*
* Author: Pavel Kozhin.
*/

package org.etherframework.behaviour.rules.controls
{
	import org.etherframework.behaviour.rules.context.IContext;
	import org.etherframework.behaviour.rules.core.Chain;
	
	import mx.rpc.IResponder;
	
	[Exclude(name="children", kind="property")]
	
	[Event(name="result", type="org.etherframework.behaviour.rules.events.FeedbackEvent")]
	
	[Event(name="fault", type="org.etherframework.behaviour.rules.events.FeedbackEvent")]
	
	public class Command extends Chain implements IInteractive, IResponder
	{
		public static const RESULT:String = "result";
		
		public static const FAULT:String = "fault";
		
		public function Command()
		{
			super();
		}
		
		private var _feedbackId:String;
		
		override public function get feedbackId():String
		{
			return (!_feedbackId) ? ((descriptor) ? descriptor.className : "") : _feedbackId; 
		}
		
		override public function set feedbackId(value:String):void
		{
			_feedbackId = value; 
		}
		
		override public function set context(value:IContext):void
		{
			super.context = value;
			
			if (value)
			{
				execute();
			}
		}
		
		public function execute():void
		{
			// should be overridden.
		}
		
		public function result(value:Object):void
		{
			context.setOutput(RESULT, value);
			dispatchFeedback(RESULT);
			context.open();
		}
		
		public function fault(value:Object):void
		{
			context.setOutput(FAULT, value);
			dispatchFeedback(FAULT);
			context.open();
		}
		
		[ArrayElementType("org.etherframework.behaviour.rules.controls.helpers.IRespondable")]
		[Inspectable(category="General", arrayType="org.etherframework.behaviour.rules.controls.helpers.IRespondable")]
		override public function get children():Array
		{
			return super.children;
		}
	}
}